package com.morenyang.webproject;

import java.sql.*;

public class DBHelper {
  private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
  private String databaseUrl, username, password;

  public String getDatabaseUrl() {
    return databaseUrl;
  }

  public void setDatabaseUrl(String databaseUrl) {
    this.databaseUrl = databaseUrl;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public DBHelper(String databaseUrl, String username, String password) {
    this.databaseUrl = databaseUrl;
    this.username = username;
    this.password = password;
  }

  private Connection getConnection() {
    Connection connection = null;
    try {
      Class.forName(DRIVER_NAME);
      connection = DriverManager.getConnection(databaseUrl, username, username);
    } catch (ClassNotFoundException | SQLException e) {
      e.printStackTrace();
    }
    return connection;
  }


  public ResultSet getResult(String sql) {
    Connection connection;
    PreparedStatement preparedStatement;
    ResultSet resultSet = null;
    try {
      connection = this.getConnection();
      preparedStatement = connection.prepareStatement(sql);
      resultSet = preparedStatement.executeQuery();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return resultSet;
  }
}
